﻿
var cityData;
var cityUrlData;
var cityPingyinData=[];   
var thisCity = {
	cityName : "",
	cityCode : "",
	bankName : "",
	bankCode : "",
	cityUrl  : "",
	changeQueue : [], //绑定在城市切换事件的函数队列
	change : function(){
		if(arguments.length>0){
			//绑定函数
			if(typeof(arguments[0]=="function")){
				this.changeQueue.push(arguments[0]);
			}
		}else{
			//按顺序执行绑定的函数
			for(var i=0; i<this.changeQueue.length; i++){
				window.setTimeout(this.changeQueue[i], 10);
			}
		}
	}
}
var thisCityUrl={
	place:"",
	placeNum:"",
	place_url:"",
	more_url:""
	
}
thisCity.change(function(){
		var bankName=thisCity.bankName;
		var bankCode=thisCity.bankCode;
        var place_url=thisCityUrl.place_url;
		var more_url=thisCityUrl.more_url;
		$(".sus-li2 a").html(bankName);
		$("#rightBranchMore").attr("href",place_url);
		$('#rightBranchName').attr('href',more_url);
	});  
function update_this_city_url(placeNum) {
	if (thisCityUrl.placeNum==placeNum) {
		
	} else if (set_thisCityUrl(placeNum)) {

 
	} 
} 
function set_thisCityUrl(placeNum){ 
	thisCityUrl.placeNum = placeNum;
	if (cityUrlData) {
		for (var i in cityUrlData) {
			if (cityUrlData[i].placeNum==thisCityUrl.placeNum) {
				//console.log(cityUrlData[i].placeNum==thisCityUrl.placeNum);
				thisCityUrl.placeNum = cityUrlData[i].placeNum;
				thisCityUrl.place = cityUrlData[i].place;
				thisCityUrl.place_url = cityUrlData[i].more_url;
                thisCityUrl.more_url=cityUrlData[i].place_url;
				return true;
			}else{
			
			
			}
		 
		
		}
	return false;
}
}
function get_cityUrlData(){

	$.ajax({
		url:"/cn/v3/js/city_url_data.js",
		dataType:"json",
		success:function(data){
		
			cityUrlData = data.cityList;
		 
		}
	});
	
}
function jsonpCallback(){}
// 城市切换（包括：更新页面头部城市名称、城市分行cookie、触发绑定在城市切换事件的函数）
function update_this_city(cityCode) {
	if (thisCity.cityCode==cityCode) {

	} else if (set_thisCity(cityCode)) {

		jQuery(document).ready(function($){
			$(".select-city>a, #shouqi").text(thisCity.cityName);//当前城市提示框
		
		});
		
		var bankCode=thisCity.bankCode;
		update_this_city_url(bankCode);
		set_city_cookie();
		
		thisCity.change();
		
	} else {
		//alert("城市切换有误");
	}  
}




// 获取省市及分行数据。如果指定了回调函数，则在成功获取数据后它会被执行。
function get_cityData(callback){
	$.ajax({
		async:false,//当跨域时此同步设置会失效
		cache:true,
		timeout:1500,
		type:"get",
		url:"http://www.ccb.com/cn/v3/js/city_bank_data.js",
		dataType:"jsonp",
		jsonp:false,
		jsonpCallback:"jsonpCallback",
		success:function(data, textStatus, jqXHR){
			cityData = data.cityList;
			if(typeof(callback)=="function"){
				callback.apply();
			}
		},
		error:function(jqXHR, textStatus, errorThrown){
			cityData = null;
		}
	});
	return ( cityData ? true : false );
}
function jsonpCallback(){}



// 设置城市及分行cookie
function set_city_cookie(){
	if ($.cookie("cityCode")!=thisCity.cityCode) {
		var cookieOptions = {expires:90, path:"/", domain:"ccb.com"};
		$.cookie("cityName", thisCity.cityName, cookieOptions);
		$.cookie("cityCode", thisCity.cityCode, cookieOptions);
		$.cookie("bankName", thisCity.bankName, cookieOptions);
		$.cookie("bankCode", thisCity.bankCode, cookieOptions);

		//$.cookie("cityUrl", thisCity.cityUrl, cookieOptions);
	}
}



// 更新变量thisCity
function set_thisCity(cityCode){
	thisCity.cityCode = cityCode;
	if (cityData) {
		for (var i in cityData) {
			if (cityData[i].cityCode==thisCity.cityCode) {
				thisCity.cityName = cityData[i].cityName;
				thisCity.bankName = cityData[i].bankName;
				thisCity.bankCode = cityData[i].bankCode;
                thisCity.cityUrl=cityData[i].cityUrl;
				return true;
			}
			var city2 = cityData[i].children;
			for (var j in city2) {
				if (city2[j].cityCode==thisCity.cityCode) {
					thisCity.cityName = city2[j].cityName;
					thisCity.bankName = city2[j].bankName;
					thisCity.bankCode = city2[j].bankCode;
					thisCity.cityUrl=cityData[i].cityUrl;
					return true;
				}
			}
		}
	}
	return false;
}



// 定位用户所在城市
function locate_city(){
	$.ajax({
		async:true,
		cache:false,
		timeout:1500,
		type:"get",
		url:"http://ebank.ccb.com/query2BankCityByIP.gsp?jsoncallback=jsonpCallback",
		dataType:"jsonp",
		success:function(data, textStatus, jqXHR){
			if (data.cityCode.length>0) {
				update_this_city(data.cityCode);
				update_this_city_url(data.bankCode);
				set_login_city_cookie('2',$.cookie("cs_cid"));
			}
		},
		error:function(jqXHR, textStatus, errorThrown){
			update_this_city("110000");//默认北京
			update_this_city_url('110000000');
			set_login_city_cookie('2',$.cookie("cs_cid"));
		}
	});
}



// 初始化用户城市
function init_this_city(){
    thisCity.change();
	get_cityData(function(){
		var cityCode = $.cookie("cityCode");
		var bankCode = $.cookie("bankCode");
		if (cityCode) {
			update_this_city(cityCode);
			update_this_city_url(bankCode);
		} else {
			locate_city();
		}
		
	}); 
    
}    
 
  get_cityUrlData(); 

jQuery(document).ready(function($){
	init_this_city();
	var $region_box = $(".select_hide");
	$(".city-seach button").click(function(){//确定搜索结果
	update_this_city($(this).siblings("#city_code_id").val());
	set_city_cookie();
    });
$(".pro_body_bg span, .pro_city3_data .off").click(function(){//选中某个城市
	$region_box.find(".pro_city3_list2:visible").andSelf().hide();
	update_this_city(this.id);
	set_login_city_cookie('0',$.cookie("cs_cid"));
	return false;
});
});

// 设置城市标志与城市会员id
function set_login_city_cookie(cityCodeFlag,cityCodeCustId){
		var cookieOptions = {expires:30, path:"/", domain:"ccb.com"};
		$.cookie("cityCodeFlag", cityCodeFlag, cookieOptions);
		$.cookie("cityCodeCustId", cityCodeCustId, cookieOptions);
}
